package cn.itcast.controller;


import cn.itcast.domain.Courier;
import cn.itcast.domain.Standard;
import cn.itcast.service.CourierService;
import cn.itcast.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CourierController {

    @Autowired
    private CourierService courierService;



    /**
     * 添加修改操作
     * @param courier
     * @return
     */
    @RequestMapping("/courier_save")
    public String save(Courier courier){
        courierService.save(courier);
        System.out.println("修改");
        return "redirect:/pages/base/courier.html";
    }


    /**
     * 删除操作
     * @param ids
     */
    @RequestMapping("/courier_delBatch")
    public String  delete(String ids){
        //分割字符串，转数组
        String[] split = ids.split(",");
         //遍历删除
        for (String s : split) {
            int i = Integer.parseInt(s);
            courierService.delete(i);
            System.out.println("删除");
        }

        return "redirect:/pages/base/courier.html";
    }

    /**
     * 还原
     * @param ids
     * @return
     */
    @RequestMapping("/courier_init")
    public String init(String ids){
        String[] split = ids.split(",");
        for (String s : split) {
            //转int类型
            int i = Integer.parseInt(s);
            courierService.init(i);
        }
        return "redirect:/pages/base/courier.html";
    }

    /**
     * 分页条件查询
     */
    @RequestMapping("/courier_pageQuery")
    @ResponseBody
    public Map PageFindAll(int page, int rows,Courier courier){
        System.out.println("page="+page+",rows="+rows);
        Page<Courier> couriers=null;
        if (courier.getCourierNum()==null&&courier.getType()==null&&courier.getCompany()==null){
            couriers = courierService.pageQuery(page,rows);

        }else {
            couriers = courierService.pageQuery(courier,page,rows);
        }

        HashMap<String, Object> result = new HashMap<>();
        result.put("total",couriers.getTotalElements());
        result.put("rows",couriers.getContent());


        return result;
    }
}
